//
//  Add_RechargeCardViewController.m
//  fxxiaoke
//
//  Created by 鞠超 on 2016/10/13.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "Add_RechargeCardViewController.h"

#import "Look_BankCardViewController.h"

@interface Add_RechargeCardViewController ()  <UIScrollViewDelegate, UITextFieldDelegate>

@property (nonatomic, strong) UIScrollView * scrollView;

@property (nonatomic, strong) UITextField * text_card;
//下一步
@property (nonatomic, strong) UIButton * btn_next;

@end

@implementation Add_RechargeCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];
    [userdefault setObject:@"1" forKey:@"Wallet_Recharge"];
    
    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void )p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    
    _lblTitle.text = @"添加银行卡";
    
    //左返回
    UIImageView * image_LeftRetrun = [[UIImageView alloc] initWithFrame:CGRectMake(11, 31.5, 25, 25)];
    image_LeftRetrun.userInteractionEnabled = YES;
    image_LeftRetrun.image = [UIImage imageNamed:@"Left-return.png"];
    [_topView addSubview:image_LeftRetrun];
    
    UILabel * lbl_return = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_LeftRetrun.frame) - 6, 35, 35, 15)];
    lbl_return.text = @"返回";
    lbl_return.textColor = [UIColor whiteColor];
    lbl_return.font = [UIFont systemFontOfSize:17];
    [_topView addSubview:lbl_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(10, 22, 60, 40);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:@"确定放弃绑定银行卡？" preferredStyle:(UIAlertControllerStyleAlert)];
    [self presentViewController:alert animated:YES completion:^{
    }];
    
    UIAlertAction * action_Cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:action_Cancel];
    
    UIAlertAction * action_OK = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        [self.navigationController popViewControllerAnimated:YES];
        
    }];
    [alert addAction:action_OK];
}

//显示tabbar
- (void )viewWillAppear:(BOOL)animated
{
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    self.scrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.contentSize = CGSizeMake(0, SCREEN_HEIGHT - 60);
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    
    UILabel * lbl_Binding_card = [[UILabel alloc] initWithFrame:CGRectMake(15, 10, SCREEN_WIDTH - 30, 15)];
    lbl_Binding_card.text = @"请绑定本人银行卡";
    lbl_Binding_card.textColor = [UIColor grayColor];
    lbl_Binding_card.font = [UIFont systemFontOfSize:15.0f];
    [self.scrollView addSubview:lbl_Binding_card];
    
    UIView * view_white = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(lbl_Binding_card.frame) + 10, SCREEN_WIDTH, 50)];
    view_white.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:view_white];
    
    UILabel * lbl_card = [[UILabel alloc] initWithFrame:CGRectMake(15, 12.5, 50, 25)];
    lbl_card.text = @"卡号";
    lbl_card.font = [UIFont systemFontOfSize:17.0f];
    lbl_card.textColor = [UIColor blackColor];
    [view_white addSubview:lbl_card];
    
    self.text_card = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(lbl_card.frame) + 20, 12.5, SCREEN_WIDTH - CGRectGetMaxX(lbl_card.frame) - 20- 10, 25)];
    self.text_card.placeholder = @"仅支持储蓄卡";
    self.text_card.font = [UIFont systemFontOfSize:17.0f];
    self.text_card.delegate = self;
    self.text_card.keyboardType = UIKeyboardTypeNumberPad;
    [view_white addSubview:self.text_card];
    [self.text_card addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    
    UILabel * lbl_lookCard = [[UILabel alloc] initWithFrame:CGRectMake(15, CGRectGetMaxY(view_white.frame) + 20, 120, 15)];
    lbl_lookCard.text = @"查看支持的银行卡";
    lbl_lookCard.font = [UIFont systemFontOfSize:15.0f];
    lbl_lookCard.textColor = [UIColor colorWithRed:90/255.0 green:167/255.0 blue:239/255.0 alpha:1.0];
    [self.scrollView addSubview:lbl_lookCard];
    
    UIButton * btn_lookCard = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btn_lookCard.frame = CGRectMake(15, CGRectGetMaxY(view_white.frame) + 13, 120, 30);
    [self.scrollView addSubview:btn_lookCard];
    [btn_lookCard addTarget:self action:@selector(btn_lookCardAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //
    self.btn_next = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.btn_next.frame = CGRectMake(15, CGRectGetMaxY(lbl_lookCard.frame) + 20, SCREEN_WIDTH - 30, 45);
    self.btn_next.layer.cornerRadius = 5;
    //226 227 228
    self.btn_next.backgroundColor = [UIColor colorWithRed:226/255.0 green:227/255.0 blue:228/255.0 alpha:1.0f];
    [self.btn_next setTitle:@"下一步" forState:(UIControlStateNormal)];
    self.btn_next.userInteractionEnabled = NO;
    [self.btn_next setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    [self.scrollView addSubview:self.btn_next];
    [self.btn_next addTarget:self action:@selector(btn_nextAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

#pragma mark - 点击查看支持的银行卡
- (void )btn_lookCardAction:(UIButton *)sender
{//NSLog(@"点击查看支持的银行卡");
    
    Look_BankCardViewController * look_BankCardViewController = [[Look_BankCardViewController alloc] init];
    [self showViewController:look_BankCardViewController sender:nil];
}

#pragma mark - 点击下一步
- (void)btn_nextAction:(UIButton *)sender
{
    NSLog(@"点击下一步,开启验证");
    
    //添加银行卡时
//    NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];
//    [userdefault setObject:@"1" forKey:@"Card_Number"];
}

#pragma mark - scrollView代理
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.text_card resignFirstResponder];
}

#pragma mark - textfield代理
-(void)textFieldDidChange:(UITextField *)theTextField
{
    if([self.text_card.text length] == 0)
    {
        self.btn_next.userInteractionEnabled = NO;
        self.btn_next.backgroundColor = [UIColor colorWithRed:226/255.0 green:227/255.0 blue:228/255.0 alpha:1.0f];
        [self.btn_next setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    }
    else
    {
        self.btn_next.userInteractionEnabled = YES;
        self.btn_next.backgroundColor = [UIColor orangeColor];
        [self.btn_next setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    }
}

@end
